package com.wanhe.viewconf.views.child;

import android.content.Context;
import android.text.TextUtils;

import com.orhanobut.logger.Logger;
import com.wanhe.base.model.disgn.Event;
import com.wanhe.base.model.disgn.ViewEvent;
import com.wanhe.base.model.holder.DataStoreMapHolder;
import com.wanhe.base.util.ObjectUtil;
import com.wanhe.viewconf.views.child.MealBookChildItemView;
import com.wanhe.viewconf.views.child.interfaces.IDataViewOperation;

import java.util.List;

/**
 * File Name:MealBookStoreView.java<br/>
 * Package Name:com.wanhe.viewconf.views.child.store<br/>
 * Date:2017/7/13 16:32<br/>
 * author:chendeji<br/>
 * desc: 用于存储和查找操作的工具控件，不显示在页面上<br/>
 * Copyright (c) 2017/7/13, 781571323@qq.com All Rights Reserved.
 *
 */
public class MealBookStoreView extends MealBookChildItemView implements IDataViewOperation {

	public static final String TAG = "MealBookStoreView";
	public MealBookStoreView(Context context) {
		super(context);
	}

	@Override
	public void storeVideo(Event event, ViewEvent.Coor coor) {
		/**
		 * 存储一些信息，将信息缓存到一个文件映射内存中
		 * 
		 * @author 陈德基
		 * @time 2017/7/13 16:35
		 */
		if (ObjectUtil.checkObject(event)) {
			DataStoreMapHolder instance = DataStoreMapHolder.getInstance();
			Event.ArgsBean args = event.getArgs();
			instance.put(args.getName(), args);
		}

	}

	@Override
	public void find(Event event, ViewEvent.Coor coor) {

		/**
		 * 查找一些存储的文件信息，将文件信息当做时间的参数然后传递给需要使用文件的控件
		 * 
		 * @author 陈德基
		 * @time 2017/7/13 16:59
		 */

		if (ObjectUtil.checkObject(event)) {
			DataStoreMapHolder instance = DataStoreMapHolder.getInstance();
			Event.ArgsBean args = event.getArgs();
			if (ObjectUtil.checkObject(args) && !TextUtils.isEmpty(args.getName())) {
				String name = args.getName();
				Object obj = instance.get(name);
				if (ObjectUtil.checkObject(obj)) {
					if (obj instanceof Event.ArgsBean) {
						Event.ArgsBean argsBean = (Event.ArgsBean) obj;
						List<Event> events = this.mItem.getEvents();
						if (ObjectUtil.checkObjectList(events)) {
							for (int i = 0; i < events.size(); i++) {
								events.get(i).setArgs(argsBean);
							}
							sendEvents(events);
						}
					}
				} else {
					Logger.t(TAG).e("数据缓存中没有找到名为：%s 的数据信息", name);
				}
			} else {
				Logger.t(TAG).e("数据存储控件 cid:%d, name:%s find方法中接收的数据有问题", this.mItem.getCid(),
						this.mItem.getName());
			}
		}
	}

	@Override
	public void store(Event event, ViewEvent.Coor coor) {
		/**
		 * 存储基础数据
		 * @author 陈德基
		 * @time 2017/7/15 15:19
		 */
		if (ObjectUtil.checkObject(event)) {
			DataStoreMapHolder instance = DataStoreMapHolder.getInstance();
			Event.ArgsBean args = event.getArgs();
			instance.put(args.getName(), args);

			callSendEventMsg();

		}
	}
}
